﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProcessor.EF;
using Hjt212Parser;
using log4net;


namespace DataProcessor.Process
{
    public class ProcessorStatus //: IProcessor
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(ProcessorStatus));

        public static void ProcessOffline(string content)
        {
            try
            {
                using (var contextTarget = new PlatformXpfContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var info = Util.GetInfoFromMn(mn);
                    if (info == null)
                    {
                        Log.Warn("MN：" + mn + " 找不到对应企业！");
                        return;
                    }
                    var masterMonitor =
                        contextTarget.主实时监控.Where(t => t.企业基本信息ID == info.企业基本信息ID)
                            .FirstOrDefault(t => t.企业排口ID == info.ID);
                    if (masterMonitor == null) return;
                    masterMonitor.在线状态ID = 1202;
                    masterMonitor.在线状态时间 = Parser.GetDateTimeByKey("DataTime", content);
                    contextTarget.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

        public static void ProcessOnline(string content)
        {
            try
            {
                using (var contextTarget = new PlatformXpfContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var info = Util.GetInfoFromMn(mn);
                    if (info == null)
                    {
                        Log.Warn("MN：" + mn + " 找不到对应企业！");
                        return;
                    }
                    var masterMonitor =
                        contextTarget.主实时监控.Where(t => t.企业基本信息ID == info.企业基本信息ID)
                            .FirstOrDefault(t => t.企业排口ID == info.ID);
                    if (masterMonitor == null) return;
                    masterMonitor.在线状态ID = 1201;
                    masterMonitor.在线状态时间 = Parser.GetDateTimeByKey("DataTime", content);
                    contextTarget.SaveChanges();
                    if (Util.DontProcess(mn))
                    {
                        ProcessOffline(content);
                    }
                }

            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

    }
}
